package cn.iocoder.yudao.module.crm.controller.admin.subject;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.enums.CommonStatusEnum;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.LayuiTreeNode;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.module.crm.controller.admin.subject.vo.*;
import cn.iocoder.yudao.module.crm.convert.subject.SubjectConvert;
import cn.iocoder.yudao.module.crm.dal.dataobject.subject.SubjectDO;
import cn.iocoder.yudao.module.crm.service.subject.SubjectService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.EXPORT;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

@Tag(name = "管理后台 - 栏目")
@RestController
@RequestMapping("/crm/subject")
@Validated
public class SubjectController {

    @Resource
    private SubjectService subjectService;

    @PostMapping("/create")
    @Operation(summary = "创建栏目")
    @PreAuthorize("@ss.hasPermission('crm:subject:create')")
    public CommonResult<Long> createSubject(@Valid @RequestBody SubjectSaveReqVO createReqVO) {
        return success(subjectService.createSubject(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新栏目")
    @PreAuthorize("@ss.hasPermission('crm:subject:update')")
    public CommonResult<Boolean> updateSubject(@Valid @RequestBody SubjectSaveReqVO updateReqVO) {
        subjectService.updateSubject(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除栏目")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('crm:subject:delete')")
    public CommonResult<Boolean> deleteSubject(@RequestParam("id") Long id) {
        subjectService.deleteSubject(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得栏目")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('crm:subject:query')")
    public CommonResult<SubjectRespVO> getSubject(@RequestParam("id") Long id) {
        SubjectDO subject = subjectService.getSubject(id);
        return success(BeanUtils.toBean(subject, SubjectRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得栏目分页")
    @PreAuthorize("@ss.hasPermission('crm:subject:query')")
    public CommonResult<PageResult<SubjectRespVO>> getSubjectPage(@Valid SubjectPageReqVO pageReqVO) {
        PageResult<SubjectDO> pageResult = subjectService.getSubjectPage(pageReqVO);
        //处理父栏目名称
        if (pageReqVO.getParentId() != null && pageReqVO.getParentId() > 0) {
            SubjectDO subject = subjectService.getSubject(pageReqVO.getParentId());
            pageReqVO.setParentName(subject.getName());
        }
        return success(new PageResult<>(SubjectConvert.INSTANCE.convertList(pageResult.getList(), pageReqVO),
                pageResult.getTotal()));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出栏目 Excel")
    @PreAuthorize("@ss.hasPermission('crm:subject:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSubjectExcel(@Valid SubjectPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SubjectDO> list = subjectService.getSubjectPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "栏目.xls", "数据", SubjectRespVO.class,
                        BeanUtils.toBean(list, SubjectRespVO.class));
    }


    @GetMapping(value = {"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取栏目精简信息列表", description = "只包含被开启的栏目，主要用于前端的下拉选项")
    public CommonResult<List<SubjectSimpleRespVO>> getSimpleSubjectList() {
        List<SubjectDO> list = subjectService.getSubjectList(
                new SubjectListReqVO().setStatus(CommonStatusEnum.ENABLE.getStatus()));
        return success(BeanUtils.toBean(list, SubjectSimpleRespVO.class));
    }

    @GetMapping(value = { "/tree"})
    @Operation(summary = "获取栏目树列表", description = "")
    public List<LayuiTreeNode> getTreeList() {
        return subjectService.buildSubjectTree();
    }

}